<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Auths;
use think\Db;
use think\Request;

class Rote
{
//    public function __construct(Request $request)
//    {
//        Auths::login_auth($request);
//    }

    /**列表加载
     * @param Request $request
     * @return \think\response\View
     */
    public function show_list(Request $request){
        $data = $this->page($request);
        return view('list',['data'=>$data['data'],'datarows'=>$data['datarows'],'tiaojian'=>$data['tiaojian']]);
    }

    /**分页
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function page(Request $request){
        $page = $request->param('page',1);
//        return $page;
        $limit = $request->param('limit',10);
        $tiaojian = $request->param('tiaojian',null);
//        return $tiaojian;
        if($tiaojian){
            $data = Db::name('rote')->where('rolename','like',"%$tiaojian%")->limit(($page-1)*$limit,$limit)->select();
            $datarows = Db::name('rote')->where('rolename','like',"%$tiaojian%")->count();
        }else{
            $data = Db::name('rote')->limit(($page-1)*$limit,$limit)->select();
            $datarows = Db::name('rote')->count();
        }
        if(count($data)){
            foreach ($data as $key=>$value){
                $jur = [];
                if(isset($value['pri_id_list'])&&count(json_decode($value['pri_id_list'],true))){
                    $jur = Db::name('privilege')->where('id','in',json_decode($value['pri_id_list'],true))->field('name')->select();
                }
                $data[$key]['jur'] = $jur;
            }
        }
        return ['data'=>$data,'datarows'=>$datarows,'tiaojian'=>$tiaojian];
    }

    /**加载添加
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_add(){
        $privilege = Db::name('privilege')->select();
        return view('add',['data'=>$privilege]);
    }

    /**添加
     * @return int|string
     */
    public function add(Request $request){
        $rolename = $request->param('rolename',null);
        if(!$rolename){
            return '请输入名称';
        }
        $pri_id_list = $request->param('pri_id_list',null);
        if(!$pri_id_list){
            return '请添加权限';
        }
        $pri_id_list = json_encode($pri_id_list);
        if(Db::name('rote')->insert(['rolename'=>$rolename,'pri_id_list'=>$pri_id_list,'state'=>1,'create_time'=>time()])){
            return 1;
        }else{
            return '添加失败';
        }
    }

    /**加载编辑页面
     * @return string|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入要编辑的数据的id';
        }
        $privilege = Db::name('privilege')->select();
        $data = Db::name('rote')->where('id',$id)->find();
        if(isset($data['pri_id_list'])&&count(json_decode($data['pri_id_list'],true))){
            $data['pri_id_list'] = json_decode($data['pri_id_list'],true);
        }
        $data['id'] = $id;
        return view('edit',['data'=>$data,'datas'=>$privilege]);
    }

    /**更新
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update(Request $request){
        $data = $request->post();
        if(!$data['rolename']){
            return '请输入角色名称';
        }
        if(!$data['id']){
            return '请输入修改的数据的id';
        }
        if(!count($data['pri_id_list'])){
            return '请输入要修改的权限';
        }
        $data['pri_id_list'] = json_encode($data['pri_id_list']);
        if(Db::name('rote')->where('id',$data['id'])->update($data)){
            return 1;
        }else{
            return '什么都没修改';
        }
    }

    /**删除
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $data = $request->post('id',null);
        if(!$data){
            return '请输入要删除的id';
        }
        if(is_array($data)){
            if(Db::name('rote')->where('id','in',$data)->delete()){
                return 1;
            }else{
                return '删除失败';
            }
        }else{
            if(Db::name('rote')->where('id',$data)->delete()){
                return 1;
            }else{
                return '删除失败';
            }
        }
    }
}